/**
 * BufferedReaderWithCallback
 *
 * Released under Apache License, Version 2.0.
 */

package bsp.helper;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class BufferedReaderWithCallback
{
	BufferedReader mReader;

	public BufferedReaderWithCallback()
	{
		/* empty by design */
	}

	public boolean open(String inputFile)
	{
		try {
			mReader = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(inputFile))), 8000);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	public boolean read(LineCallback callback)
	{
		String str;
		try {
			while ( (str = mReader.readLine()) != null ) {
				callback.process(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	public boolean close()
	{
		try {
			mReader.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
}

/* end */
